package cn.alise.util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

import cn.alise.consts.ConfigConsts;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import common.Logger;

/**
 * ClassName:PDFUtils <br/>
 * Description: 导出pdf文件工具类 <br/>
 * Date: 2016年12月30日 上午9:44:48 <br/>
 *
 * @author 陈昊
 * @version 1.0
 * @since JDK 1.7
 * @see
 */
public class PDFUtils {
    
	private static Logger logger = Logger.getLogger(CSVUtils.class);
	
	private static int maxWidth = 450;
	
    private static Font keyfont;// 设置字体大小    
    private static Font textfont;// 设置字体大小
    private static Font title1font;// 设置字体大小    
    private static Font title2font;// 设置字体大小    
	
	static{    
        BaseFont bfChinese;    
        try {    
            bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    
            keyfont = new Font(bfChinese, 11, Font.NORMAL);// 设置字体大小    
            textfont = new Font(bfChinese, 11, Font.NORMAL);// 设置字体大小
            title1font = new Font(bfChinese, 18, Font.BOLD);// 设置字体大小    
            title2font = new Font(bfChinese, 14, Font.BOLD);// 设置字体大小    
        } catch (Exception e) {             
            e.printStackTrace();    
        }     
    }
	
	public static File createFile(String fileName) throws Exception{
		
		UUID uuid = UUID.randomUUID();
    	fileName = fileName + uuid;
    	
    	// 获取保存路径
    	String outPutPath = ConfigConsts.lacp_EXPORT_PATH;
    	
    	File file = new File(outPutPath + "/" + fileName + ".pdf");
		File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
        file.createNewFile();
        
		return file;
	}
	
	/**
	 * 中文转换
	 * str :要转换的内容     c:字体大小
	 */
	public static Paragraph getChinese(String str,int c) {
		BaseFont bfChinese = null;
		try {
			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",                BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {
			logger.error("DocumentException", e);
		} catch (IOException e) {
			logger.error("IOException", e);
		}
		Font FontChinese = new Font(bfChinese, c, Font.NORMAL);// 设置为中文
		Paragraph pragraph=new Paragraph(str, FontChinese);
		return pragraph;
	}
	
	public static PdfPCell createCell(String value,int align){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);            
        cell.setHorizontalAlignment(align);
        if (StringUtils.isEmpty(value)) {
        	cell.setPhrase(new Phrase("-",textfont));
        } else {
        	cell.setPhrase(new Phrase(value,textfont));    
        }
        return cell;    
    }
	
	public static PdfPCell createCell(String value){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        if (StringUtils.isEmpty(value)) {
        	cell.setPhrase(new Phrase("-",textfont));
        } else {
        	cell.setPhrase(new Phrase(value,textfont));
        }
        return cell;    
    }
	
	public static PdfPCell createCell(String value,int align,int colspan){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        cell.setHorizontalAlignment(align);        
        cell.setColspan(colspan);    
        if (StringUtils.isEmpty(value)) {
        	cell.setPhrase(new Phrase("-",textfont));
        } else {
        	cell.setPhrase(new Phrase(value,textfont));
        }
        return cell;    
    }
	
	public static PdfPCell createTitle1Cell(String value,int align,int colspan,boolean boderFlag){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        cell.setHorizontalAlignment(align);        
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, title1font));    
        cell.setPadding(6.0f);
        if (!boderFlag) {    
            cell.setBorder(0);    
            cell.setPaddingTop(15.0f);    
            cell.setPaddingBottom(8.0f);    
        }    
        return cell;    
    }
	
	public static PdfPCell createTitle2Cell(String value,int align,int colspan,boolean boderFlag){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        cell.setHorizontalAlignment(align);        
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value,title2font));    
        cell.setPadding(12.0f);
        if (!boderFlag) {    
            cell.setBorder(0);    
            cell.setPaddingTop(15.0f);    
            cell.setPaddingBottom(8.0f);    
        }    
        return cell;    
    }
	
	public static PdfPCell createCell(String value,int align,int colspan,boolean boderFlag){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        cell.setHorizontalAlignment(align);        
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value,keyfont));    
        cell.setPadding(6.0f);
        if (!boderFlag) {    
            cell.setBorder(0);    
            cell.setPaddingTop(15.0f);    
            cell.setPaddingBottom(8.0f);    
        }    
        return cell;    
    }
	
	public static PdfPCell createCell(String value,int align,int colspan,boolean boderFlag, String fw){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        cell.setHorizontalAlignment(align);        
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value,keyfont));    
        cell.setPadding(6.0f);
        if (!boderFlag) {    
            cell.setBorder(0);    
            cell.setPaddingTop(15.0f);    
            cell.setPaddingBottom(8.0f);    
        }
        if ("top".equals(fw)) {
        	cell.setBorderWidthTop(0);
        } else if ("bottom".equals(fw)) {
        	cell.setBorderWidthBottom(0);
        } else if ("top-bottom".equals(fw)) {
        	cell.setBorderWidthTop(0);
        	cell.setBorderWidthBottom(0);
        }
        return cell;    
    }
	
	public static PdfPCell createImgCell(String url, int colspan) throws Exception {
		
        PdfPCell cell = new PdfPCell(getImage(url));
        cell.setColspan(colspan);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);    
        return cell;    
    }
	
	public static PdfPTable getImage(String url) throws Exception {
		
	    PdfPTable datatable = new PdfPTable(1);
	    datatable.getDefaultCell().setBorder(1);
	    datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
	
	    if (StringUtils.isEmpty(url)) {
	    	PdfPCell pCTitle = new PdfPCell();
	    	pCTitle.setPhrase(new Phrase("-",textfont)); 
	    	pCTitle.setColspan(1);
    	    pCTitle.setBorder(1);
	    	pCTitle.setVerticalAlignment(Element.ALIGN_MIDDLE);    
	    	pCTitle.setHorizontalAlignment(Element.ALIGN_CENTER);
    	    datatable.addCell(pCTitle);
        } else {
        	Image jpg = Image.getInstance(url); 
    	    float heigth=jpg.getHeight();  
            float width=jpg.getWidth();
            int percent = getPercent(heigth, width);
            jpg.scalePercent(percent);
    	    jpg.setAlignment(Image.MIDDLE);  
    	    PdfPCell pCTitle = new PdfPCell(jpg);
    	    pCTitle.setColspan(1);
    	    pCTitle.setBorder(1);
    	    pCTitle.setVerticalAlignment(Element.ALIGN_MIDDLE);    
    	    pCTitle.setHorizontalAlignment(Element.ALIGN_CENTER);    
    	    datatable.addCell(pCTitle);
        }
	    
	    return datatable;
	}
	
	public static int getPercent(float h,float w) {  
        int p=0;  
        float p2=0.0f;  
        if(h>w) {  
            p2=267/h*100;  
        } else {  
            p2=180/w*100;  
        }  
        p=Math.round(p2);  
        return p;  
    }    
	
	public static PdfPTable createTable(int colNumber){    
        PdfPTable table = new PdfPTable(colNumber);    
        try{    
            table.setTotalWidth(maxWidth);    
            table.setLockedWidth(true);    
            table.setHorizontalAlignment(Element.ALIGN_CENTER);         
            table.getDefaultCell().setBorder(1);    
        }catch(Exception e){    
            e.printStackTrace();    
        }    
        return table;    
    }
	
	public static PdfPTable createTable(float[] widths){
        PdfPTable table = new PdfPTable(widths);    
        try {    
            table.setTotalWidth(maxWidth);    
            table.setLockedWidth(true);    
            table.setHorizontalAlignment(Element.ALIGN_CENTER);         
            table.getDefaultCell().setBorder(1);    
        } catch(Exception e) {    
            e.printStackTrace();    
        }    
        return table;    
    }
	
	public static PdfPTable createBlankTable(){    
        PdfPTable table = new PdfPTable(1);    
        table.getDefaultCell().setBorder(0);    
        table.addCell(createCell(""));             
        table.setSpacingAfter(20.0f);    
        table.setSpacingBefore(20.0f);    
        return table;    
    }
}